package com.example.project_springboot.service.impl;

import com.example.project_springboot.aop.Log;
import com.example.project_springboot.mapper.DeptMapper;
import com.example.project_springboot.pojo.Dept;
import com.example.project_springboot.pojo.DeptLog;
import com.example.project_springboot.service.DeptLogService;
import com.example.project_springboot.service.DeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    DeptMapper deptMapper;
    @Resource
    DeptLogService deptLogService;
    @Override
    public List<Dept> findAllDept() {
        return deptMapper.find();
    }

    @Override
    @Log
    public void insertDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDept(dept);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    @Log
    public void deleteById(Integer id) {
        try{
        deptMapper.deleteById(id);
        id= id/0;
        deptMapper.deleteByEmpId(id);}
        finally {
            DeptLog log = new DeptLog();
            log.setCreateTime(LocalDateTime.now());
            log.setDescription("执行了删除部门操作,解散的是"+id+"部门");
            deptLogService.insert(log);
        }
    }

    @Override
    @Log
    public void update(Dept dept){
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public Dept selectById(Integer id){
        return deptMapper.selectById(id);
    }
}
